﻿//**************************************************
//CLASS : tcdc_mst_product
//AUTHOR : PATTARAWUT CHANNGAM & SUCHIN INPIRUT
//LAST MODIFIED : Monday, February 25, 2013
//**************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Model
{

public class tcdc_mst_product
{

     private DBUtil DB;
     public string strConn { get; set; }
     public int product_id {get;set;}
     public string product_name {get;set;}
     public double price {get;set;}
     public string uom {get;set;}
     public int category_id {get;set;}
     public string create_date {get;set;}
     public int create_by {get;set;}
     public string update_date {get;set;}
     public int update_by {get;set;}

     public tcdc_mst_product() {
          this.DB = new DBUtil();
          DB.strConn = strConn;
     }

     public DataTable getById(string id) {

          DataTable dt = new DataTable();
          this.DB = new DBUtil();
          DB.strConn = strConn;

          string sql = "SELECT * FROM tcdc_mst_product" + 
                       " WHERE product_id = '" + id + "'";

          dt = DB.ExecuteReader(sql);

          return dt;

     }

     public DataTable getByObject(tcdc_mst_product obj) {

          DataTable dt = new DataTable();
          this.DB = new DBUtil();
          DB.strConn = strConn;

          string sql = "SELECT * FROM tcdc_mst_product " + 
                       "WHERE 1 = 1 ";

          if(obj.product_id != null && 
             obj.product_id.ToString() != null && 
             obj.product_id.ToString() != "0")
          {
               sql += "AND product_id LIKE '%" + obj.product_id + "%' ";
          }
          if(obj.product_name != null && 
             obj.product_name.ToString() != null && 
             obj.product_name.ToString() != "0")
          {
               sql += "AND product_name LIKE '%" + obj.product_name + "%' ";
          }
          if(obj.price != null && 
             obj.price.ToString() != null && 
             obj.price.ToString() != "0")
          {
               sql += "AND price LIKE '%" + obj.price + "%' ";
          }
          if(obj.uom != null && 
             obj.uom.ToString() != null && 
             obj.uom.ToString() != "0")
          {
               sql += "AND uom LIKE '%" + obj.uom + "%' ";
          }
          if(obj.category_id != null && 
             obj.category_id.ToString() != null && 
             obj.category_id.ToString() != "0")
          {
               sql += "AND category_id LIKE '%" + obj.category_id + "%' ";
          }
          if(obj.create_date != null && 
             obj.create_date.ToString() != null && 
             obj.create_date.ToString() != "0")
          {
               sql += "AND create_date LIKE '%" + obj.create_date + "%' ";
          }
          if(obj.create_by != null && 
             obj.create_by.ToString() != null && 
             obj.create_by.ToString() != "0")
          {
               sql += "AND create_by LIKE '%" + obj.create_by + "%' ";
          }
          if(obj.update_date != null && 
             obj.update_date.ToString() != null && 
             obj.update_date.ToString() != "0")
          {
               sql += "AND update_date LIKE '%" + obj.update_date + "%' ";
          }
          if(obj.update_by != null && 
             obj.update_by.ToString() != null && 
             obj.update_by.ToString() != "0")
          {
               sql += "AND update_by LIKE '%" + obj.update_by + "%' ";
          }
          dt = DB.ExecuteReader(sql);

          return dt;

     }

     public int insertData(tcdc_mst_product obj) {

          this.DB = new DBUtil();
          DB.strConn = strConn;

          SqlParameter[] prm;
          prm = new SqlParameter[9];

          prm[0] = new SqlParameter("@product_name", SqlDbType.NVarChar,100);
          if(obj.product_name != null && 
             obj.product_name.ToString() != null && 
             obj.product_name.ToString() != "0")
          {
               prm[0].Value = obj.product_name;
          }
          else
          {
               prm[0].Value = DBNull.Value;
          }

          prm[1] = new SqlParameter("@price", SqlDbType.Decimal);
          prm[1].Precision = 18;
          prm[1].Scale = 10;
          if(obj.price != null && 
             obj.price.ToString() != null && 
             obj.price.ToString() != "0")
          {
               prm[1].Value = obj.price;
          }
          else
          {
               prm[1].Value = DBNull.Value;
          }

          prm[2] = new SqlParameter("@uom", SqlDbType.NVarChar,50);
          if(obj.uom != null && 
             obj.uom.ToString() != null && 
             obj.uom.ToString() != "0")
          {
               prm[2].Value = obj.uom;
          }
          else
          {
               prm[2].Value = DBNull.Value;
          }

          prm[3] = new SqlParameter("@category_id", SqlDbType.Int);
          if(obj.category_id != null && 
             obj.category_id.ToString() != null && 
             obj.category_id.ToString() != "0")
          {
               prm[3].Value = obj.category_id;
          }
          else
          {
               prm[3].Value = DBNull.Value;
          }

          prm[4] = new SqlParameter("@create_date", SqlDbType.DateTime);
          if(obj.create_date != null && 
             obj.create_date.ToString() != null && 
             obj.create_date.ToString() != "0")
          {
               prm[4].Value = obj.create_date;
          }
          else
          {
               prm[4].Value = DBNull.Value;
          }

          prm[5] = new SqlParameter("@create_by", SqlDbType.Int);
          if(obj.create_by != null && 
             obj.create_by.ToString() != null && 
             obj.create_by.ToString() != "0")
          {
               prm[5].Value = obj.create_by;
          }
          else
          {
               prm[5].Value = DBNull.Value;
          }

          prm[6] = new SqlParameter("@update_date", SqlDbType.DateTime);
          if(obj.update_date != null && 
             obj.update_date.ToString() != null && 
             obj.update_date.ToString() != "0")
          {
               prm[6].Value = obj.update_date;
          }
          else
          {
               prm[6].Value = DBNull.Value;
          }

          prm[7] = new SqlParameter("@update_by", SqlDbType.Int);
          if(obj.update_by != null && 
             obj.update_by.ToString() != null && 
             obj.update_by.ToString() != "0")
          {
               prm[7].Value = obj.update_by;
          }
          else
          {
               prm[7].Value = DBNull.Value;
          }

          prm[8] = new SqlParameter("@ReturnValue", SqlDbType.Int);
          prm[8].Direction = ParameterDirection.Output;

          DB.ExecuteNonQueryProc("sp_tcdc_mst_product_insert", prm, false);
          return (Int32)prm[8].Value;
     }

     public int updateData(tcdc_mst_product obj) {

          this.DB = new DBUtil();
          DB.strConn = strConn;

          SqlParameter[] prm;
          prm = new SqlParameter[9];

          prm[0] = new SqlParameter("@product_id", SqlDbType.Int);
          if(obj.product_id != null && 
             obj.product_id.ToString() != null && 
             obj.product_id.ToString() != "0")
          {
               prm[0].Value = obj.product_id;
          }
          else
          {
               prm[0].Value = DBNull.Value;
          }
          prm[1] = new SqlParameter("@product_name", SqlDbType.NVarChar,100);
          if(obj.product_name != null && 
             obj.product_name.ToString() != null && 
             obj.product_name.ToString() != "0")
          {
               prm[1].Value = obj.product_name;
          }
          else
          {
               prm[1].Value = DBNull.Value;
          }
          prm[2] = new SqlParameter("@price", SqlDbType.Decimal);
          prm[2].Precision = 18;
          prm[2].Scale = 10;
          if(obj.price != null && 
             obj.price.ToString() != null && 
             obj.price.ToString() != "0")
          {
               prm[2].Value = obj.price;
          }
          else
          {
               prm[2].Value = DBNull.Value;
          }
          prm[3] = new SqlParameter("@uom", SqlDbType.NVarChar,50);
          if(obj.uom != null && 
             obj.uom.ToString() != null && 
             obj.uom.ToString() != "0")
          {
               prm[3].Value = obj.uom;
          }
          else
          {
               prm[3].Value = DBNull.Value;
          }
          prm[4] = new SqlParameter("@category_id", SqlDbType.Int);
          if(obj.category_id != null && 
             obj.category_id.ToString() != null && 
             obj.category_id.ToString() != "0")
          {
               prm[4].Value = obj.category_id;
          }
          else
          {
               prm[4].Value = DBNull.Value;
          }
          prm[5] = new SqlParameter("@create_date", SqlDbType.DateTime);
          if(obj.create_date != null && 
             obj.create_date.ToString() != null && 
             obj.create_date.ToString() != "0")
          {
               prm[5].Value = obj.create_date;
          }
          else
          {
               prm[5].Value = DBNull.Value;
          }
          prm[6] = new SqlParameter("@create_by", SqlDbType.Int);
          if(obj.create_by != null && 
             obj.create_by.ToString() != null && 
             obj.create_by.ToString() != "0")
          {
               prm[6].Value = obj.create_by;
          }
          else
          {
               prm[6].Value = DBNull.Value;
          }
          prm[7] = new SqlParameter("@update_date", SqlDbType.DateTime);
          if(obj.update_date != null && 
             obj.update_date.ToString() != null && 
             obj.update_date.ToString() != "0")
          {
               prm[7].Value = obj.update_date;
          }
          else
          {
               prm[7].Value = DBNull.Value;
          }
          prm[8] = new SqlParameter("@update_by", SqlDbType.Int);
          if(obj.update_by != null && 
             obj.update_by.ToString() != null && 
             obj.update_by.ToString() != "0")
          {
               prm[8].Value = obj.update_by;
          }
          else
          {
               prm[8].Value = DBNull.Value;
          }

          return DB.ExecuteNonQueryProc("sp_tcdc_mst_product_update", prm, false);
     }

     public int deleteData(string id) {

          this.DB = new DBUtil();
          DB.strConn = strConn;

          SqlParameter[] prm;
          prm = new SqlParameter[1];

          prm[0] = new SqlParameter("@product_id", id);

          return DB.ExecuteNonQueryProc("sp_tcdc_mst_product_delete", prm, false);
     }
}

}

